/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#ifndef PLANET_WARS_2D_RT_CORE_TESTS_CHECK_OBSERVERS_GENERIC_H
#define PLANET_WARS_2D_RT_CORE_TESTS_CHECK_OBSERVERS_GENERIC_H


#include <check.h>
#include "PlanetWars2dRT-core/utils/observer_creators.h"


typedef bool   (*add_observer_t)    (void* an_observable,
				     const observer_t* an_observer);
typedef void   (*remove_observers_t)(void* an_observable);
typedef void   (*notify_observers_t)(void* an_observable, void* data);
typedef size_t (*get_nb_observers_t)(void* an_observable);


void
test_add_observer_generic(void* an_observable,
			  add_observer_t add_observer)
{
  struct observer_t an_observer;
  an_observer = observer_useless_create();
  ck_assert(add_observer(an_observable, &an_observer));
}

void
test_remove_observers_generic(void*              an_observable,
			      add_observer_t     add_observer,
			      remove_observers_t remove_observers,
			      get_nb_observers_t get_nb_observers)
{
  remove_observers(an_observable);
  ck_assert(get_nb_observers(an_observable) == 0);
  
  struct observer_t an_observer;
  an_observer = observer_useless_create();
  
  ck_assert(add_observer(an_observable, &an_observer));
  ck_assert(get_nb_observers(an_observable) == 1);
  remove_observers(an_observable);
  ck_assert(get_nb_observers(an_observable) == 0);
  
  ck_assert(add_observer(an_observable, &an_observer));
  ck_assert(add_observer(an_observable, &an_observer));
  ck_assert(get_nb_observers(an_observable) == 2);
  remove_observers(an_observable);
  ck_assert(get_nb_observers(an_observable) == 0);
  
  ck_assert(add_observer(an_observable, &an_observer));
  ck_assert(add_observer(an_observable, &an_observer));
  ck_assert(add_observer(an_observable, &an_observer));
  ck_assert(get_nb_observers(an_observable) == 3);
  remove_observers(an_observable);
  ck_assert(get_nb_observers(an_observable) == 0);
}

void
test_notify_observers_generic(void*              an_observable,
			      add_observer_t     add_observer,
			      notify_observers_t notify_observers)
{
  size_t count1 = 0, count2 = 0;
  struct observer_t an_observer;
  
  an_observer = observer_counter_create(&count1);
  ck_assert(add_observer(an_observable, &an_observer));
  
  ck_assert(count1 == 0);
  
  notify_observers(an_observable, NULL);
  ck_assert(count1 == 1);
  ck_assert(count2 == 0);
  
  notify_observers(an_observable, NULL);
  ck_assert(count1 == 2);
  ck_assert(count2 == 0);
  
  an_observer = observer_counter_create(&count2);
  ck_assert(add_observer(an_observable, &an_observer));
  
  notify_observers(an_observable, NULL);
  ck_assert(count1 == 3);
  ck_assert(count2 == 1);
  
  notify_observers(an_observable, NULL);
  ck_assert(count1 == 4);
  ck_assert(count2 == 2);
}


#endif
